package com.tjec.project.system.service.impl;

import com.tjec.common.constant.Constants;
import com.tjec.common.utils.DateUtils;
import com.tjec.common.utils.ServletUtils;
import com.tjec.framework.security.service.TokenService;
import com.tjec.project.system.domain.BmsAttachment;
import com.tjec.project.system.domain.BmsAttachmentHistory;
import com.tjec.project.system.domain.vo.BmsAttachmentVo;
import com.tjec.project.system.mapper.BmsAttachmentMapper;
import com.tjec.project.system.service.IBmsAttachmentHistoryService;
import com.tjec.project.system.service.IBmsAttachmentService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * 文件Service业务层处理
 *
 * @author tjec
 * @date 2020-04-27
 */
@Service
public class BmsAttachmentServiceImpl implements IBmsAttachmentService {
    @Autowired
    private BmsAttachmentMapper bmsAttachmentMapper;
    @Autowired
    private TokenService tokenService;
    @Resource
    private IBmsAttachmentHistoryService iBmsAttachmentHistoryService;

    @Value("${sftp.http}")
    private String http;
    @Value("${sftp.ip}")
    private String ip;

    /**
     * 查询文件
     *
     * @param guid 文件ID
     * @return 文件
     */
    @Override
    public BmsAttachment selectBmsAttachmentByGuid(String guid) {
        return bmsAttachmentMapper.selectBmsAttachmentById(guid);
    }

    /**
     * 查询文件列表
     *
     * @param bmsAttachment 文件
     * @return 文件
     */
    @Override
    public List<BmsAttachment> selectBmsAttachmentList(BmsAttachment bmsAttachment) {
        return bmsAttachmentMapper.selectBmsAttachmentList(bmsAttachment);
    }

    /**
     * 查询下载文件列表
     *
     * @param guids 需要下载的文件id
     * @return 文件
     */
    @Override
    public List<BmsAttachmentVo> selectBmsAttachmentDownload(String guids) {
        String[] ss = guids.split(",");
        List<BmsAttachmentVo> list = bmsAttachmentMapper.selectBmsAttachmentDownload(ss, "", "");
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                item.setStoragePath(http + ip + Constants.RESOURCE_PREFIX + item.getStoragePath());
            });

        }
        return list;
    }

    @Override
    public List<BmsAttachmentVo> selectBmsAttachmentDownloadabsolute(String guid) {
        String[] ss = guid.split(",");
        List<BmsAttachmentVo> list = bmsAttachmentMapper.selectBmsAttachmentDownload(ss, "", "");
        return list;
    }

    @Override
    public List<BmsAttachmentVo> selectBmsAttachmentDownloadByBizId(String bizId, String category) {
        List<BmsAttachmentVo> list = bmsAttachmentMapper.selectBmsAttachmentDownload(null, bizId, category);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                item.setStoragePath(http + ip +Constants.RESOURCE_PREFIX + item.getStoragePath());
            });

        }
        return list;
    }
    
    @Override
    public List<BmsAttachmentVo> selectBmsAttachmentDownloadByBizIdNew(String bizId, String category) {
        List<BmsAttachmentVo> list = bmsAttachmentMapper.selectBmsAttachmentDownloadNew(null, bizId, category);
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(item -> {
                item.setStoragePath(http + ip +Constants.RESOURCE_PREFIX + item.getStoragePath());
            });

        }
        return list;
    }

    /**
     * 新增文件
     *
     * @param bmsAttachment 文件
     * @return 结果
     */
    @Override
    public int insertBmsAttachment(BmsAttachment bmsAttachment) {
        String guid = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        bmsAttachment.setCreateBy(guid);
        bmsAttachment.setCreateTime(DateUtils.getNowDate());
        return bmsAttachmentMapper.insertBmsAttachment(bmsAttachment);
    }

    /**
     * 修改文件
     *
     * @param bmsAttachment 文件
     * @return 结果
     */
    @Override
    public int updateBmsAttachment(BmsAttachment bmsAttachment) {
        String guid = tokenService.getLoginUser(ServletUtils.getRequest()).getUser().getUserId();
        bmsAttachment.setUpdateBy(guid);
        bmsAttachment.setUpdateTime(DateUtils.getNowDate());
        return bmsAttachmentMapper.updateBmsAttachment(bmsAttachment);
    }

    /**
     * 批量删除文件
     *
     * @param guids 需要删除的文件ID
     * @return 结果
     */
    @Override
    public int deleteBmsAttachmentByIds(String[] guids) {
        return bmsAttachmentMapper.deleteBmsAttachmentByIds(guids);
    }

    /**
     * 删除文件信息
     *
     * @param guid 文件ID
     * @return 结果
     */
    @Override
    public int deleteBmsAttachmentById(String guid) {
        return bmsAttachmentMapper.deleteBmsAttachmentById(guid);
    }

    /**
     * 将新的文件信息更新到原来的记录中
     *
     * @param oldGuid
     * @param newGuid
     * @return
     */
    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, rollbackFor = Exception.class)
    public Boolean updateBmsAttachment(String oldGuid, String newGuid) {
        Boolean ret = false;

        BmsAttachment oldBmsAttachment = this.selectBmsAttachmentByGuid(oldGuid);
        BmsAttachment newBmsAttachment = this.selectBmsAttachmentByGuid(newGuid);
        if (null != oldBmsAttachment && null != newBmsAttachment) {
            //原本的复制一份到历史表
            BmsAttachmentHistory history = new BmsAttachmentHistory();
            BeanUtils.copyProperties(oldBmsAttachment, history);
            history.setAttachId(oldGuid);
            int historyCount = iBmsAttachmentHistoryService.insertBmsAttachmentHistory(history);
            if (historyCount > -1) {
                //将新的文件信息更新到原来的记录中
                newBmsAttachment.setGuid(oldGuid);
                int oldCount = this.updateBmsAttachment(newBmsAttachment);
                //删除新的文件
                int newCount = this.deleteBmsAttachmentById(newGuid);
                if (oldCount > -1 && newCount > -1) {
                    ret = true;
                }
            }

        }
        return ret;
    }

	@Override
	public List<BmsAttachment> selectBmsAttachmentByBizId(String bizId) {
		return bmsAttachmentMapper.selectBmsAttachmentByBizId(bizId);
	}

	@Override
	public List<BmsAttachmentVo> selectBmsAttachmentDownload(String[] guids) {
		return bmsAttachmentMapper.selectBmsAttachmentDownload(guids, "", "");
	}
}
